package Ventanas;

import Gestores.Entidades.EntidadAfiliado;
import Gestores.Entidades.EntidadCurso;
import Gestores.Entidades.EntidadEmail;
import Gestores.Entidades.EntidadPermiso;
import Gestores.Entidades.EntidadTelefono;
import Gestores.Entidades.EntidadTituloAcademico;
import Gestores.Entidades.EntidadTituloProvisionNacional;
import Negocios.AfiliadoNegocio;
import Negocios.CursoNegocio;
import Negocios.EmailNegocio;
import Ventana.Interfaces.LogError;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import Negocios.InscripcionNegocio;
import Negocios.TelefonoNegocio;
import Negocios.TituloAcademicoNegocio;
import Negocios.TituloProvisionNacionalNegocio;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class VAfiliado extends javax.swing.JFrame implements LogError {

    public static String ID_AFILIADO;
    public static boolean MODIFICACION_ON;
    /*Axiliares*/
    private ArrayList<Object> auxTelefonos;
    private ArrayList<Object> auxEmails;
    private ArrayList<Object> auxCursos;

    /*Clases de negocios*/
    private AfiliadoNegocio negocioAfiliado;
    private TelefonoNegocio negocioTelefono;
    private EmailNegocio negocioEmail;
    private TituloAcademicoNegocio negocioTituloAcademico;
    private TituloProvisionNacionalNegocio negocioTituloProvisionNacional;
    private CursoNegocio negocioCurso;
    /* Objetos almacenadores */
    private ArrayList<Object> listaTelefonos;
    private ArrayList<Object> listaEmails;
    private ArrayList<Object> listaCursos;
    /* Usuarios */
    private EntidadPermiso Permiso;

    public VAfiliado() {
        initComponents();

        listaTelefonos = new ArrayList<Object>();
        listaEmails = new ArrayList<Object>();
        listaCursos = new ArrayList<Object>();
        negocioAfiliado = new AfiliadoNegocio();
        negocioTelefono = new TelefonoNegocio();
        negocioEmail = new EmailNegocio();
        negocioTituloAcademico = new TituloAcademicoNegocio();
        negocioTituloProvisionNacional = new TituloProvisionNacionalNegocio();
        negocioCurso = new CursoNegocio();
    }

    public VAfiliado(EntidadPermiso Permiso) {
        initComponents();

        this.Permiso = Permiso;
        configurarFrame();

        listaTelefonos = new ArrayList<Object>();
        listaEmails = new ArrayList<Object>();
        listaCursos = new ArrayList<Object>();
        negocioAfiliado = new AfiliadoNegocio();
        negocioTelefono = new TelefonoNegocio();
        negocioEmail = new EmailNegocio();
        negocioTituloAcademico = new TituloAcademicoNegocio();
        negocioTituloProvisionNacional = new TituloProvisionNacionalNegocio();
        negocioCurso = new CursoNegocio();
    }

    private void setComponentsDisabled() {
        this.jButton1.setEnabled(false);
        this.jButton2.setEnabled(false);
        this.jButton3.setEnabled(false);
        this.jButton4.setEnabled(false);
        this.jButton5.setEnabled(false);
        this.jButton6.setEnabled(false);
        this.jButton7.setEnabled(false);
        this.jComboBox2.setEnabled(false);
        this.jTextField1.setEnabled(false);
        this.jTextField10.setEnabled(false);
        this.jTextField11.setEnabled(false);
        this.jTextField13.setEnabled(false);
        this.jTextField14.setEnabled(false);
        this.jTextField15.setEnabled(false);
        this.jTextField16.setEnabled(false);
        this.jTextField2.setEnabled(false);
        this.jTextField3.setEnabled(false);
        this.jTextField4.setEnabled(false);
        this.jTextField6.setEnabled(false);
        this.jTextField7.setEnabled(false);
        this.jTextField8.setEnabled(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jTextField6 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jTextField13 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jTextField14 = new javax.swing.JTextField();
        jTextField15 = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        jTextField16 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registro de Nuevo Afiliado");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel9.setText("Dirección:");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel11.setText("CÓDIGO DE AFILIADO:");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(51, 51, 255));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "------", "Soltero/a", "Casado/a", "Divorciado/a", "Viudo/a" }));

        jLabel8.setText("Teléfono/Celular:");

        jLabel7.setText("Estado Civil:");

        jLabel1.setText("Nombres:");

        jLabel3.setText("Apellido Materno:");

        jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField2FocusLost(evt);
            }
        });

        jLabel2.setText("Apellido Paterno:");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("-----     DATOS PERSONALES DEL AFILIADO     -----");
        jLabel5.setToolTipText("");

        jLabel4.setText("Nº Cédula de Identidad:");

        jLabel15.setText("E-Mail:");

        jButton5.setText("Gestión Telefonos");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Gestión Correos");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel15)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton6)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel15)
                    .addComponent(jButton5)
                    .addComponent(jButton6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("-----     DATOS PROFESIONALES     -----");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Título Académico"));

        jLabel13.setText("Número:");

        jLabel14.setText("Título Académico de:");

        jLabel16.setText("Universidad:");

        jLabel17.setText("País:");

        jLabel19.setText("Fecha de Entrega:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField8))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField11))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel19)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 559, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Título de Provisión Nacional"));

        jLabel20.setText("Título en  Provisión Nacional de:");

        jLabel21.setText("Número:");

        jLabel22.setText("Fecha otorgado:");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel21)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel22)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 430, Short.MAX_VALUE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel20)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField15)))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22)
                    .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Cursos realizados"));

        jButton7.setText("Gestión Cursos");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jButton7)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jButton1.setText("Registrar Nuevo Afiliado");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar proceso");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Búsquedas");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Modificar Datos Afiliado");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            String errorMessage = "";
            if (jTextField1.getText().isEmpty()) {
                errorMessage += "Falta el NOMBRE del nuevo afiliado.\n";
            }

            // El apellido paterno no se validara, debido a que un afiliado podria no tenerlo.

            if (jTextField3.getText().isEmpty()) {
                errorMessage += "Falta el APELLIDO PATERNO del nuevo afiliado.\n";
            }
            if (jTextField4.getText().isEmpty()) {
                errorMessage += "Falta el Numero de C.I. del nuevo afiliado.\n";
            }
            if (jComboBox2.getSelectedIndex() == 0) {
                errorMessage += "No ha especificado el ESTADO CIVIL del nuevo afiliado.\n";
            }

            /* Validacion de Telefonos */
            if (listaTelefonos.isEmpty()) {
                errorMessage += "No ha especificado por lo menos un NUMERO de telefono.\n";
            }

            /* Validacion de Emails */
            if (listaEmails.isEmpty()) {
                errorMessage += "No se ha especificado por lo menos un CORREO.\n";
            }

            if (jTextField6.getText().isEmpty()) {
                errorMessage += "Falta la DIRECCION donde vive el nuevo afiliado.\n";
            }
            if (jTextField7.getText().isEmpty()) {
                errorMessage += "Falta el NUMERO del TITULO ACADEMICO del nuevo afiliado.\n";
            }
            if (jTextField8.getText().isEmpty()) {
                errorMessage += "Falta la DESCRIPCION del TITULO ACADEMICO del nuevo afiliado.\n";
            }
            if (jTextField10.getText().isEmpty()) {
                errorMessage += "Falta la UNIVERSIDAD de procedencia del TITULO ACADEMICO.\n";
            }
            if (jTextField11.getText().isEmpty()) {
                errorMessage += "Falta el PAIS donde pertenece la UNIVERSIDAD.\n";
            }
            if (jTextField13.getText().isEmpty()) {
                errorMessage += "Falta la FECHA en que otorgo el TITULO ACADEMICO.\n";
            }
            if (jTextField14.getText().isEmpty()) {
                errorMessage += "Falta el NUMERO del TITULO EN PROVISION NACIONAL del nuevo afiliado.\n";
            }
            if (jTextField16.getText().isEmpty()) {
                errorMessage += "Falta la FECHA en que se otorgo el TITULO EN PROVISION NACIONAL.\n";
            }
            if (jTextField15.getText().isEmpty()) {
                errorMessage += "Falta la DESCRIPCION del TITULO EN PROVISION NACIONAL.\n";
            }

            InscripcionNegocio validar = new InscripcionNegocio();
            if (errorMessage.equals("")) {
                if (validar.verificarPagos(jTextField4.getText())) {
                    validacionExistenciaAfiliado();
                    validacionExistenciaTelefonos();
                    validacionExistenciaEmails();
                    validacionExistenciaTituloAcademico();
                    validacionExistenciaTituloProvisionNacional();
                    //validacionExistenciaCursos();

                    procesoRegistroAfiliado();
                    procesoRegistroTelefono();
                    procesoRegistroEmail();
                    procesoRegistroTituloAcademico();
                    procesoregistroTituloProvisionNacional();
                    procesoRegistroCurso();

                    JOptionPane.showMessageDialog(this, "Nuevo Afiliado registrado sin problemas.", "Nuevo Afiliado", JOptionPane.INFORMATION_MESSAGE);
                    this.dispose();
                } else {
                    throw new Exception("El Nuevo Afiliado no ha realizado sus respectivos pagos.");
                }
            } else {
                mostrarError(errorMessage);
            }
        } catch (Exception error) {
            mostrarError(error.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        VAfiliadoBusqueda vAfiliadoBusqueda = new VAfiliadoBusqueda();
        vAfiliadoBusqueda.enlazar(negocioAfiliado);
        vAfiliadoBusqueda.setVisible(true);
        vAfiliadoBusqueda.requestFocus();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTextField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField2FocusLost
        try {
            jLabel6.setText(negocioAfiliado.generarCodigo(jTextField2.getText()));
        } catch (Exception error) {
            mostrarError(error.getMessage());
        }
    }//GEN-LAST:event_jTextField2FocusLost

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        try {
            procesoModificarAfiliado();
            procesoModificarTelefonos();
            procesoModificarCorreos();
            procesoModificarTA();
            procesoModificarTPN();
            procesoModificarCursos();

            JOptionPane.showMessageDialog(this, "Cambios guardados!!!", "Modificacion Afiliado", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } catch (Exception e) {
            if (e.getMessage().contains("input")) {
                JOptionPane.showMessageDialog(this, "Realize una busqueda de algun Afiliado antes de Modificar", "Modificar Afiliado", JOptionPane.INFORMATION_MESSAGE);
            } else {
                mostrarError(e.getMessage());
            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        VTelefonos vTelefonos = new VTelefonos();
        vTelefonos.setLocationRelativeTo(this);
        vTelefonos.setVisible(true);
        vTelefonos.recibirVariableListaTelefonos(jLabel6.getText(), listaTelefonos, negocioTelefono);
        vTelefonos.requestFocus();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        VEmail ventanaEmail = new VEmail();
        ventanaEmail.recibirReferencias(jLabel6.getText(), listaEmails, negocioEmail);
        ventanaEmail.setVisible(true);
        ventanaEmail.requestFocus();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        VCurso ventana = new VCurso();
        ventana.setLocationRelativeTo(this);
        ventana.enlazar(listaCursos, negocioCurso, jLabel6.getText());
        ventana.setVisible(true);
        ventana.requestFocus();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        if (MODIFICACION_ON) {
            try {
                cargarAfiliado();
                cargarTelefonos();
                cargarCorreos();
                cargarTA();
                cargarTPN();
                cargarCursos();
                jButton4.setEnabled(true);
                ID_AFILIADO = null;
                MODIFICACION_ON = false;
            } catch (Exception ex) {
                mostrarError(ex.getMessage());
            }
        }
    }//GEN-LAST:event_formWindowGainedFocus

    private void configurarFrame() {
        if (this.Permiso.isAnadir()) {
            this.jButton1.setEnabled(true);
            this.jButton5.setEnabled(true);
            this.jButton6.setEnabled(true);
            this.jButton7.setEnabled(true);
            this.jTextField1.setEnabled(true);
            this.jTextField10.setEnabled(true);
            this.jTextField11.setEnabled(true);
            this.jTextField13.setEnabled(true);
            this.jTextField14.setEnabled(true);
            this.jTextField15.setEnabled(true);
            this.jTextField16.setEnabled(true);
            this.jTextField2.setEnabled(true);
            this.jTextField3.setEnabled(true);
            this.jTextField4.setEnabled(true);
            this.jTextField6.setEnabled(true);
            this.jTextField7.setEnabled(true);
            this.jTextField8.setEnabled(true);
        }
        if (this.Permiso.isBuscar()) {
            this.jButton3.setEnabled(true);
        }
        if (this.Permiso.isEditar()) {
            this.jButton4.setEnabled(true);
        }
        if (this.Permiso.isEliminar()) {
            this.jButton4.setEnabled(true);
        }
    }

    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VAfiliado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VAfiliado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VAfiliado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VAfiliado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new VAfiliado().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mostrarError(String mensaje) {
        JOptionPane.showMessageDialog(this, mensaje, "Error Afiliado", JOptionPane.ERROR_MESSAGE);
    }

    @Override
    public void cargarCredemcial(Object object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void procesoRegistroAfiliado() throws Exception {
        /* Registro de datos del Afiliado */
        EntidadAfiliado eAfiliado = new EntidadAfiliado();
        eAfiliado.setId(jLabel6.getText());
        eAfiliado.setCi(Integer.parseInt(jTextField4.getText()));
        eAfiliado.setNombres(jTextField1.getText());
        eAfiliado.setApellidoPaterno(jTextField2.getText());
        eAfiliado.setApellidoMaterno(jTextField3.getText());
        eAfiliado.setDireccion(jTextField6.getText());
        char ec = ((String) jComboBox2.getSelectedItem()).charAt(0);
        eAfiliado.setEstadoCivil("" + ec);
        this.negocioAfiliado.registrarNuevo(eAfiliado);
        /* ------------------------------ */
    }

    private void procesoRegistroTelefono() throws Exception {
        /* Registro de Telefonos del Afiliado */
        for (int i = 0; i < listaTelefonos.size(); i++) {
            EntidadTelefono eTelefono = (EntidadTelefono) listaTelefonos.get(i);
            negocioTelefono.registrar(eTelefono);
        }
        /* ---------------------------------- */
    }

    private void procesoRegistroEmail() throws Exception {
        /* Registro de Telefonos del Afiliado */
        for (int i = 0; i < listaEmails.size(); i++) {
            EntidadEmail eEmail = (EntidadEmail) listaEmails.get(i);
            negocioEmail.registrar(eEmail);
        }
        /* ---------------------------------- */
    }

    private void procesoRegistroTituloAcademico() throws Exception {
        EntidadTituloAcademico eta = new EntidadTituloAcademico();
        eta.setNumero(Integer.parseInt(jTextField7.getText()));
        eta.setId_afiliado(jLabel6.getText());
        eta.setDescripcion(jTextField8.getText());
        eta.setUniversidad(jTextField10.getText());
        eta.setPais(jTextField11.getText());

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        Date fecha = (Date) format.parse(jTextField3.getText());
//        eta.setFecha(fecha);
        String t = jTextField13.getText();
        java.util.Date fecha = new java.util.Date(t);
        Date fO = new Date(fecha.getTime());
        eta.setFecha(fO);

        negocioTituloAcademico.insertar(eta);
    }

    private void procesoregistroTituloProvisionNacional() throws Exception {
        EntidadTituloProvisionNacional etpn = new EntidadTituloProvisionNacional();
        etpn.setNumero(Integer.parseInt(jTextField14.getText()));
        etpn.setIdAfiliado(jLabel6.getText());
        etpn.setDescripcion(jTextField15.getText());
//        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
//        Date fecha = (Date) formato.parse(jTextField16.getText());
//        etpn.setFecha(fecha);
        String t = jTextField16.getText();
        java.util.Date fecha = new java.util.Date(t);
        Date fO = new Date(fecha.getTime());
        etpn.setFecha(fO);

        negocioTituloProvisionNacional.insertar(etpn);
    }

    private void procesoRegistroCurso() throws Exception {
        for (int i = 0; i < listaCursos.size(); i++) {
            EntidadCurso ec = (EntidadCurso) listaCursos.get(i);
            negocioCurso.insertar(ec);
        }
    }

    private void validacionExistenciaAfiliado() throws Exception {
        if (negocioAfiliado.existe(jLabel6.getText())) {
            throw new Exception("El Afiliado con código " + jLabel6.getText() + " ya existe en el sistema.");
        }
    }

    private void validacionExistenciaTelefonos() throws Exception {
        for (int i = 0; i < listaTelefonos.size(); i++) {
            EntidadTelefono et = (EntidadTelefono) listaTelefonos.get(i);
            if (negocioTelefono.existe(et.getNumero())) {
                throw new Exception("El numero " + et.getNumero() + " ya se encuentra registrado en el sistema.");
            }
        }
    }

    private void validacionExistenciaEmails() throws Exception {
        for (int i = 0; i < listaEmails.size(); i++) {
            EntidadEmail ee = (EntidadEmail) listaEmails.get(i);
            if (negocioEmail.existe(ee.getMail())) {
                throw new Exception("El Correo " + ee.getMail() + " ya se encuentra en el Sistema.");
            }
        }
    }

    private void validacionExistenciaTituloAcademico() throws Exception {
        if (negocioTituloAcademico.existe(Integer.parseInt(jTextField7.getText()))) {
            throw new Exception("El Numero del Titulo Academico " + jTextField7.getText() + " ya se encuentra en el Sistema.");
        }
    }

    private void validacionExistenciaTituloProvisionNacional() throws Exception {
        if (negocioTituloProvisionNacional.existe(Integer.parseInt(jTextField14.getText()))) {
            throw new Exception("El Número " + jTextField14.getText() + " del Titulo de Provision Nacional ya se encuentra en el Sistema.");
        }
    }

    private void validacionExistenciaCursos() throws Exception {
        for (int i = 0; i < listaCursos.size(); i++) {
            EntidadCurso ec = (EntidadCurso) listaCursos.get(i);
            if (negocioCurso.existe(ec.getId())) {
                throw new Exception("El Curso con Id " + ec.getId() + " ya se encuentra en el sistema");
            }
        }
    }

    private void cargarAfiliado() throws Exception {
        if (MODIFICACION_ON && (ID_AFILIADO != null)) {
            ArrayList<Object> l = negocioAfiliado.buscarPorId(ID_AFILIADO);
            if (l.size() > 1) {
                throw new Exception("Inconsistencia en la Busqueda.");
            }
            EntidadAfiliado ea = (EntidadAfiliado) l.get(0);
            jLabel6.setText(ID_AFILIADO);
            jTextField1.setText(ea.getNombres());
            jTextField2.setText(ea.getApellidoPaterno());
            jTextField3.setText(ea.getApellidoMaterno());
            jTextField4.setText(String.valueOf(ea.getCi()));

            for (int i = 0; i < jComboBox2.getItemCount(); i++) {
                char c = ((String) jComboBox2.getItemAt(i)).charAt(0);
                if ((c & ea.getEstadoCivil().charAt(0)) == 1) {
                    jComboBox2.setSelectedIndex(i + 1);
                    break;
                }
            }
            jTextField6.setText(ea.getDireccion());
        } else {
            throw new Exception("Modificación: Sin datos de referencia(Afiliado).");
        }
    }

    private void cargarTelefonos() throws Exception {
        if (MODIFICACION_ON && (ID_AFILIADO != null)) {
            listaTelefonos = negocioTelefono.buscarPorIdAfiliado(ID_AFILIADO);
            auxTelefonos = negocioTelefono.buscarPorIdAfiliado(ID_AFILIADO);
        } else {
            throw new Exception("Modificación: Sin datos de referencia(Telefonos).");
        }
    }

    private void cargarCorreos() throws Exception {
        if (MODIFICACION_ON && (ID_AFILIADO != null)) {
            listaEmails = negocioEmail.buscarPorIdAfiliado(ID_AFILIADO);
            auxEmails = negocioEmail.buscarPorIdAfiliado(ID_AFILIADO);
        } else {
            throw new Exception("Modificación: Sin datos de referencia(Correos).");
        }
    }

    private void cargarTA() throws Exception {
        if (MODIFICACION_ON && (ID_AFILIADO != null)) {
            ArrayList<Object> l = negocioTituloAcademico.buscarPorIdAfiliado(ID_AFILIADO);

            if (l.size() == 1) {
                EntidadTituloAcademico eta = (EntidadTituloAcademico) l.get(0);
                jTextField7.setText(String.valueOf(eta.getNumero()));
                jTextField8.setText(eta.getDescripcion());
                jTextField10.setText(eta.getUniversidad());
                jTextField11.setText(eta.getPais());
                SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
                String fecha = formato.format(eta.getFecha());
                jTextField13.setText(fecha);
            } else {
                throw new Exception("Inconsistencia en la Busqueda.");
            }
        } else {
            throw new Exception("Modificación: Sin datos de referencia(Titulo Academico).");
        }
    }

    private void cargarTPN() throws Exception {
        if (MODIFICACION_ON && (ID_AFILIADO != null)) {
            ArrayList<Object> l = negocioTituloProvisionNacional.buscarPorId(ID_AFILIADO);
            if (l.size() == 1) {
                EntidadTituloProvisionNacional etpn = (EntidadTituloProvisionNacional) l.get(0);
                jTextField14.setText(String.valueOf(etpn.getNumero()));
                SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
                String fecha = formato.format(etpn.getFecha());
                jTextField16.setText(fecha);
                jTextField15.setText(etpn.getDescripcion());
            } else {
                throw new Exception("Inconsistencia en la Busqueda.");
            }
        } else {
            throw new Exception("Modificación: Sin datos de referencia(Titulo Provision Nacional).");
        }
    }

    private void cargarCursos() throws Exception {
        if (MODIFICACION_ON && (ID_AFILIADO != null)) {
            listaCursos = negocioCurso.buscarPorIdAfiliado(ID_AFILIADO);
            auxCursos = negocioCurso.buscarPorIdAfiliado(ID_AFILIADO);
        } else {
            throw new Exception("Modificación: Sin datos de referencia(Cursos).");
        }
    }

    private void procesoModificarAfiliado() throws Exception {
        EntidadAfiliado eAfiliado = new EntidadAfiliado();
        eAfiliado.setId(jLabel6.getText());
        eAfiliado.setCi(Integer.parseInt(jTextField4.getText()));
        eAfiliado.setNombres(jTextField1.getText());
        eAfiliado.setApellidoPaterno(jTextField2.getText());
        eAfiliado.setApellidoMaterno(jTextField3.getText());
        eAfiliado.setDireccion(jTextField6.getText());
        char ec = ((String) jComboBox2.getSelectedItem()).charAt(0);
        eAfiliado.setEstadoCivil("" + ec);

        negocioAfiliado.actualizar(jLabel6.getText(), eAfiliado);
    }

    private void procesoModificarTelefonos() throws Exception {
        for (int i = 0; i < auxTelefonos.size(); i++) {
            EntidadTelefono et = (EntidadTelefono) auxTelefonos.get(i);
            negocioTelefono.eliminar(et);
        }
        for (int i = 0; i < listaTelefonos.size(); i++) {
            EntidadTelefono et = (EntidadTelefono) listaTelefonos.get(i);
            negocioTelefono.registrar(et);
        }
    }

    private void procesoModificarCorreos() throws Exception {
        for (int i = 0; i < auxEmails.size(); i++) {
            EntidadEmail ee = (EntidadEmail) auxEmails.get(i);
            negocioEmail.eliminar(ee);
        }
        for (int i = 0; i < listaEmails.size(); i++) {
            EntidadEmail ee = (EntidadEmail) listaEmails.get(i);
            negocioEmail.registrar(ee);
        }
    }

    private void procesoModificarTA() throws Exception {
        EntidadTituloAcademico eta = new EntidadTituloAcademico();
        eta.setNumero(Integer.parseInt(jTextField7.getText()));
        eta.setId_afiliado(jLabel6.getText());
        eta.setDescripcion(jTextField8.getText());
        eta.setUniversidad(jTextField10.getText());
        eta.setPais(jTextField11.getText());
//        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
//        Date fecha = (Date) formato.parse(jTextField13.getText());
//        eta.setFecha(fecha);

        String t = jTextField13.getText();
        java.util.Date fecha = new java.util.Date(t);
        Date fO = new Date(fecha.getTime());
        eta.setFecha(fO);

//        eta.setFecha(new Date(new java.util.Date(jTextField13.getText()).getTime()));

        negocioTituloAcademico.actualizar(jLabel6.getText(), eta);
    }

    private void procesoModificarTPN() throws Exception {
        EntidadTituloProvisionNacional etpn = new EntidadTituloProvisionNacional();
        etpn.setNumero(Integer.parseInt(jTextField14.getText()));
        etpn.setIdAfiliado(jLabel6.getText());
        etpn.setDescripcion(jTextField15.getText());
//        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
//        Date fecha = (Date) formato.parse(jTextField16.getText());
//        etpn.setFecha(fecha);
//        etpn.setFecha(new Date(new java.util.Date(jTextField16.getText()).getTime()));
        String t = jTextField16.getText();
        java.util.Date fecha = new java.util.Date(t);
        Date fO = new Date(fecha.getTime());
        etpn.setFecha(fO);
// CAMBIAR EN LA BASE DE DATOS.
        negocioTituloProvisionNacional.actualizar(jLabel6.getText(), etpn);
    }

    private void procesoModificarCursos() throws Exception {
        for (int i = 0; i < auxCursos.size(); i++) {
            EntidadCurso ec = (EntidadCurso) auxCursos.get(i);
            negocioCurso.eliminar(ec);
        }
        for (int i = 0; i < listaCursos.size(); i++) {
            EntidadCurso ec = (EntidadCurso) listaCursos.get(i);
            negocioCurso.insertar(ec);
        }
    }
}
